//
//  JiBenXinXiViewController.m
//  跨界收
//
//  Created by Allen on 2018/11/12.
//  Copyright © 2018年 gztx. All rights reserved.
//

#import "JiBenXinXiViewController.h"
#import "JiBenXinXiTableViewCell.h"
#import "JiBenXinXiView.h"

@interface JiBenXinXiViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (weak, nonatomic) IBOutlet UIButton *secondButton;
@property (weak, nonatomic) IBOutlet UIButton *fistButton;
@property (nonatomic, strong) NSArray *sectionArray;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *contentArray;
@property (nonatomic, strong) NSArray *placeholderArray;
@property (nonatomic, strong) NSArray *tagArray;


@property (nonatomic, strong) NSMutableArray *contents;
@property (nonatomic, strong) NSMutableArray *contents1;
@end

@implementation JiBenXinXiViewController
- (IBAction)fistclick:(id)sender {
}
- (IBAction)secondClick:(id)sender {
}
- (void)initData
{
    _tagArray = @[@"100",@"101",@"102",@"103",@"104",@"105",@"200",@"201",@"202",@"203",@"204"];
    _sectionArray = @[@"商户信息",@"联系人信息"];
    _titleArray = @[@"商户全称",@"商户简称",@"客服电话",@"所属行业",@"经营地址",@"详细地址"];
    _contentArray = @[@"姓名",@"职务",@"手机号",@"邮箱",@"微信号"];
    _placeholderArray = @[@"例：广州XX科技公司",@"例：便利店",@"固话或手机均可",@"请选择",@"请选择省市区",@"请输入详细地址"];
    _contents = [NSMutableArray arrayWithCapacity:_titleArray.count];
    for (int i = 0; i < _titleArray.count; i++) {
        [_contents addObject:@""];
    }
    _contents1 = [NSMutableArray arrayWithCapacity:_contentArray.count];
    for (int i = 0; i < _contentArray.count; i++) {
        [_contents1 addObject:@""];
    }
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"签约管理";
    [self initData];
    [self setup];
    // Do any additional setup after loading the view from its nib.
}
- (void)setup
{
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle =UITableViewCellSeparatorStyleNone;
    [self.tableview registerNib:[UINib nibWithNibName:@"JiBenXinXiTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
    header.backgroundColor = [UIColor whiteColor];
    JiBenXinXiView *view = [JiBenXinXiView shareInstance];
    for (id label in view.oneView.subviews) {
        if ([label isKindOfClass:[UILabel class]]) {
            UILabel *oneLabel = (UILabel *)label;
            oneLabel.textColor = [UIColor cz_colorWithHex:0x239fdb];
        }
    }
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 80);
    [header addSubview:view];
    self.tableview.tableHeaderView = header;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentTextFieldDidEndEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
}
#pragma mark -- UITableViewDelegate/DataSource --
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 300, 30)];
    label.text = _sectionArray[section];
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:16];
    [headerview addSubview:label];
    return headerview;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   return 30;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sectionArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return _titleArray.count;
    }else
        return _contentArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    JiBenXinXiTableViewCell *customCell = (JiBenXinXiTableViewCell *)cell;
    
    if (indexPath.section == 0) {
        customCell.titleLabel.text = _titleArray[indexPath.row];
        customCell.customTextField.placeholder = [NSString stringWithFormat:@"%@",_placeholderArray[indexPath.row]];
        customCell.customTextField.text = _contents[indexPath.row];
    } else {
        // 切记：对于cell的重用，有if，就必须有else。因为之前屏幕上出现的cell离开屏幕被缓存起来时候，cell上的内容并没有清空，当cell被重用时，系统并不会给我们把cell上之前配置的内容清空掉，所以我们在else中对contentTextField内容进行重新配置或者清空（根据自己的业务场景而定）
        customCell.titleLabel.text = _contentArray[indexPath.row];
        customCell.customTextField.placeholder = [NSString stringWithFormat:@"请输入%@",_contentArray[indexPath.row]];
        customCell.customTextField.text = _contents1[indexPath.row];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    JiBenXinXiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[JiBenXinXiTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    cell.customTextField.delegate = self;
    WeakSelf;
    switch (indexPath.section) {
        case 0:
        {
            
            cell.block = ^(NSString * text) {
                // 更新数据源x
                [weakSelf.contents replaceObjectAtIndex:indexPath.row withObject:text];
            };

            if (indexPath.row == 3 || indexPath.row == 4) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
            cell.customTextField.tag = 100+indexPath.row;
        }
            break;
        case 1:
        {
            cell.block = ^(NSString * text) {
                // 更新数据源x
                [weakSelf.contents1 replaceObjectAtIndex:indexPath.row withObject:text];
            };
            cell.customTextField.tag = 200+indexPath.row;
        }
            break;
        default:
            break;
    }
//    cell.customTextField.indexPath = indexPath;
    return cell;
}

#pragma mark - private method
- (void)contentTextFieldDidEndEditing:(NSNotification *)noti {
    UITextField *textField = noti.object;
//    if (textField.indexPath.section == 0) {
//        NSString *text = textField.text;
//        NSInteger row =textField.indexPath.row;
//
//        if (text && text.length) {
//            [self.contents replaceObjectAtIndex:row withObject:text];
//        }
//    } else if (textField.indexPath.section == 1) {
//        // 同上，请自行脑补
//    } else if (textField.indexPath.section == 2) {
//        // 同上，请自行脑补
//    } else {
//        // 同上，请自行脑补
//    }
}



- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
//    NSString *tag = [NSString stringWithFormat:@"%ld",(long)textField.tag]; 下一版本添加 从上到下填下
//    NSMutableArray *pArray = [self reManageData:tag];
//    NSLog(@";pArray%@",pArray);
//    if (pArray.count>0) {//不是第一个输入框
//        for (NSString *tags in pArray) {
//            NSInteger taga = [tags integerValue];
//            if (taga-200>0) {
//                NSIndexPath *pIndexPath = [NSIndexPath indexPathForRow:taga-200 inSection:1];
//                NSString *nS = _contents1[pIndexPath.row];
//                if (nS.length == 0) {
//                    [self showMBProgressHUDTextWithMessage:[NSString stringWithFormat:@"%@",_contents1[pIndexPath.row]]];
//                    return NO;
//                }
//            }else{
//                NSIndexPath *pIndexPath = [NSIndexPath indexPathForRow:taga-100 inSection:0];
//                JiBenXinXiTableViewCell *cell = (JiBenXinXiTableViewCell *)[self.tableview cellForRowAtIndexPath:pIndexPath];
//                NSString *nS = _contents[pIndexPath.row];
//                if (nS.length == 0) {
//                    [self showMBProgressHUDTextWithMessage:[NSString stringWithFormat:@"请先填写%@",cell.titleLabel.text]];
//                    return NO;
//                }
//            }
//        }
//    }
    
    if (textField.tag == 103 || textField.tag == 104) {
        return NO;
    }else
        return YES;
}
- (NSMutableArray *)reManageData:(NSString *)tag
{
    NSMutableArray *pArray = [NSMutableArray array];//得到当前输入框之前的tag数组
    for (NSInteger i=0; i<_tagArray.count; i++) {
        if ([tag isEqualToString:_tagArray[i]]) {
            break;
        }
        [pArray addObject:_tagArray[i]];
    }
    return pArray;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    

}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSString *tag = [NSString stringWithFormat:@"%ld",(long)textField.tag];
}
@end
